﻿/********************************************************************************/
// <copyright file="EventRepository.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="24/12/2012 3:00:46 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using JLL.China.Infrastructure.Repositories.Admin;
using JLL.China.Model.DTO;

namespace JLL.China.Infrastructure.Repositories.Event
{
    public class EventRepository : SQLRepositoryBase<Model.Event, int>, IEventRepository
    {
        public override Model.Event FindBy(int key)
        {
            throw new NotImplementedException();
        }

        public override IList<Model.Event> FindAll()
        {
            var evt = DataContext.Events.OrderBy(x => x.DateCreated).ToList();
            if (evt != null)
            {
                var allEvents = new List<Model.Event>();
                foreach (var e in evt)
                {
                    allEvents.Add(EventFactory.FromLINQObject(e));
                }
                return allEvents;
            }
            else
            {
                return new List<Model.Event>();
            }
        }

        public  Model.Event FindById(decimal? Id)
        {
            var _event = DataContext.Events.SingleOrDefault(x => Id == x.ID);
            if (_event != null)
                return EventFactory.FromLINQObject(_event);
            else
                return null;
        }

        public  Model.Event FindByIDandLanguageFid(decimal? eventId,decimal languageFid)
        {
            if (eventId==null)
            {
                return null;
            }
            var _event = DataContext.Events.SingleOrDefault(x => eventId == x.Event_ID && languageFid == x.LanguageFID);
            if (_event != null)
                return EventFactory.FromLINQObject(_event);
            else
                return null;
        }

      

        /// <summary>
        /// create a new event 1 english and 1 chinese
        /// </summary>
        /// <param name="e">model event</param>
        /// <returns></returns>
        public decimal CreateNewEvent(Model.Event e)
        {
            
            try
            {
                int event_ID = DataContext.sp_event_create(e.Event_ID, e.Admin.ID, e.Title, e.EventType, e.EventSummary
                                                       ,e.EventContent, e.EventDate, e.EventColor, e.EventDateEnd,
                                                       e.LanguageFID, e.EventTime, e.EventTimeEnd, e.Where, e.Status,
                                                       e.ContactEmail, e.DateCreated, e.CreatedBy, e.DateUpdated);
                return event_ID;
            }
            catch (Exception)
            {

                return 0;
            }
        }

        /// <summary>
        /// update an event
        /// </summary>
        /// <param name="e">model event</param>
        /// <returns></returns>
        public decimal UpdateEvent(Model.Event e)
        {
            if (e.Event_ID != 0)
            {
                try
                {
                    int event_ID = DataContext.sp_event_update(e.Event_ID, e.Admin.ID, e.Title, e.EventType, e.EventSummary,
                                                           e.EventContent, e.EventDate, e.EventColor, e.EventDateEnd,
                                                           e.LanguageFID, e.EventTime, e.EventTimeEnd, e.Where, e.Status,
                                                           e.ContactEmail, e.DateCreated, e.CreatedBy, e.DateUpdated);
                    return event_ID;
                }
                catch (Exception exception)
                {
                    return 0;
                }
            }
            return 0;
        }

        /// <summary>
        /// delete event
        /// </summary>
        /// <param name="eventID"> event id</param>
        /// <returns></returns>
        public int DeleteEvent(decimal eventID)
        {
            try
            {
                DataContext.sp_event_delete(eventID);
            }
            catch (Exception)
            {
                return 0;
            }

            return 1;
        }

        public void ChangeStatus(string arrayId, bool? status)
        {
            status = (status == null) ? false : status;
            DataContext.sp_event_change_status(arrayId, status);
        }

        public void RemoveSelected(string arrayId)
        {
            DataContext.sp_event_remove_selected(arrayId);
        }

        protected override void PersistDeletedItem(Model.Event item)
        {
            var _event = DataContext.Events.SingleOrDefault(x => x.Event_ID == item.ID);
            if(_event != null)
                DataContext.Events.DeleteOnSubmit(_event);
        }

        protected override void PersistNewItem(Model.Event item)
        {
            var _event = EventFactory.ToLINQObject(item);
            DataContext.Events.InsertOnSubmit(_event);
        }

        protected override void PersistUpdatedItem(Model.Event item)
        {
            var existEvent = DataContext.Events.SingleOrDefault(x => x.Event_ID == item.ID);
            if (existEvent != null)
            {
                var _event = item;
                existEvent.Title = _event.Title;
                existEvent.EventType = _event.EventType;
                existEvent.EventSummary = _event.EventSummary;
                existEvent.EventContent = _event.EventContent;
                existEvent.EventDate = _event.EventDate;
                existEvent.EventTime = _event.EventTime;
                existEvent.EventDateEnd = _event.EventDateEnd;
                existEvent.EventTimeEnd = _event.EventTimeEnd;
               // existEvent.Venue = _event.Venue;
                existEvent.Status = _event.Status;
                existEvent.ContactEmail = _event.ContactEmail;
                existEvent.DateCreated = _event.DateCreated;
                existEvent.CreatedBy = _event.CreatedBy;
                existEvent.AdminFID = _event.Admin.ID;
                existEvent.DateUpdated = _event.DateUpdated;
            }
            else
            {
                throw new Exception("Event not found to update");
            }
        }
       


        #region IEventRepository Members

        public IList<Model.Event> Search(EventSearchCriteria searchCriteria)
        {
            var items = from m in DataContext.Events
                        where m.LanguageFID == 1 //get only english version
                        select m ;
            if (!searchCriteria.Descending)
            {
                switch (searchCriteria.OrderBy)
                {
                    case EventSearchOrder.Status:
                        items = items.OrderBy(x => x.Status);
                        break;
                    default:
                        items = items.OrderBy(x => x.Title);
                        break;
                }
            }
            else
            {
                switch (searchCriteria.OrderBy)
                {
                    case EventSearchOrder.Status:
                        items = items.OrderByDescending(x => x.Status);
                        break;
                    default:
                        items = items.OrderByDescending(x => x.Title);
                        break;
                }
            }

            var list = items.Skip(searchCriteria.PageIndex * searchCriteria.Count).Take(searchCriteria.Count).ToList();
            var result = new List<Model.Event>();
            foreach (var n in list)
            {
                result.Add(EventFactory.FromLINQObject(n));
            }
            return result;
        }

      

        #endregion
    }
}
